package com.hr.approval.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hr.approval.entity.ApprovalData;
import com.hr.approval.entity.param.ApprovalParam;
import com.hr.approval.entity.vo.ApprovalDataDisplay;
import com.hr.approval.entity.vo.ApprovalDataVO;
import com.hr.approval.service.ApprovalDataService;
import com.hr.common.retrun.R;
import com.hr.system.entity.dto.SysUserDto;
import com.hr.system.service.SysUserService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hr
 * @since 2024-05-07
 */
@RestController
@RequestMapping("/approval-data")
@CrossOrigin
public class ApprovalDataController {

    @Autowired
    private ApprovalDataService approvalDataService;

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private RocketMQTemplate template;
    @PostMapping("/add")
    public R add(@RequestBody ApprovalData approvalData){
        template.convertAndSend("hr_work_approval",approvalData);
        return R.ok("ok");
    }

    @PostMapping("/update")
    public R update(@RequestBody ApprovalData approvalData){
        return R.ok(approvalDataService.update(approvalData));
    }

    @GetMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id){
        approvalDataService.delete(id);
        return R.ok(null);
    }

    /**
     * 管理员看所有数据
     * @return
     */
    @GetMapping("/list")
    public R getApprovalDataList() {
        List<ApprovalData> approvalData = approvalDataService.getApprovalDataList();
        return R.ok(approvalData);
    }
    @GetMapping("/getMyApprovalList")
    public R getMyApprovalList(ApprovalData approvalData) {
        List<ApprovalData> approvalDatas = approvalDataService.getMyApprovalList(approvalData);
        return R.ok(approvalDatas);
    }
    @GetMapping("/list-report")
    public R getApprovalDataListByReportId(ApprovalParam approvalParam, PageInfo<ApprovalDataVO> pageInfo) {
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getSize());
        List<ApprovalData> unApprove = approvalDataService.getApproveDataByUserId(approvalParam);
        return R.ok(new PageInfo<>(unApprove));
    }

//    @GetMapping("/list-report/{id}")
//    public R getApprovalDataListByReportId(@PathVariable("id") Integer id) {
//        List<ApprovalData> approvalDataListByReportId = approvalDataService.getApprovalDataListByReportId(id);
//        return R.ok(approvalDataListByReportId);
//    }

//    @GetMapping("/list-approval/{id}")
//    public R getApprovalDataListByApproval(@PathVariable("id") Integer id) {
//        List<ApprovalData> approvalData = approvalDataService.getApprovalDataListByApproval(id);
//        return R.ok(approvalData);
//    }


    /**
     * 获取当前审批人的未审批和已审批数据
     * @return
     */
    @GetMapping("/query-data")
    public R getApproveDataByApproval(ApprovalParam approvalParam, PageInfo<ApprovalDataVO> pageInfo){
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getSize());
        List<ApprovalDataVO> unApprove = approvalDataService.getApproveDataByApproval(approvalParam);
        return R.ok(new PageInfo<>(unApprove));

    }

    /**
     * 同意审批
     * @param id
     * @return
     */
    @GetMapping("/agree")
    public R agreeApprove(@RequestParam("id") Integer id, @RequestParam("state") Integer state) {
        Integer i = approvalDataService.agreeApprove(id,state);
        return R.ok(i);
    }

    /**
     * 驳回审批
     * @param id
     * @return
     */
    @GetMapping("/disagree/{id}")
    public R disagreeApprove(@PathVariable("id") Integer id) {
        Integer i = approvalDataService.disagreeApprove(id);
        return R.ok(i);
    }

    /**
     * 展示单个数据，用于查看和编辑
     * 审批人未转成姓名
     * @param id
     * @return
     */
    @GetMapping("/display/{id}")
    public R display(@PathVariable("id") Integer id) {
        ApprovalDataDisplay approvalData = approvalDataService.getApprovalDataById(id);
        return R.ok(approvalData);
    }

}
